package com.fernfx.xingtan.my.presenter;

import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseIRequestCallback;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.my.contract.PrivateSettingContract;
import com.fernfx.xingtan.my.entity.GetPrivateSettingEntity;
import com.fernfx.xingtan.my.model.PrivateSettingModel;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;
import com.suke.widget.SwitchButton;

import java.util.Map;

/**
 * Created by admin on 2018/8/21.
 */

public class PrivateSettingPresenter<P extends PrivateSettingContract.View> extends BaseIRequestCallback implements PrivateSettingContract.Presenter {
    private P P;
    private PrivateSettingContract.Model model;

    @Override
    public void init(BaseView view) {
        this.P = (P) view;
        this.model = new PrivateSettingModel();
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {
        P.getActivity().showLoading();
        model.request(requestArgsMap,this);
    }



    @Override
    public void detachView() {
        if (model != null) {
            model = null;
        }
    }

    @Override
    public void onSuccessful(NetworkOKResult result) {
        P.getActivity().hideLoading();
        String message =result.getMessage();
        GetPrivateSettingEntity getPrivateSettingEntity = FastJsonUtil.fromBean(message, GetPrivateSettingEntity.class);
        if (OtherUtil.checkRequestStatus(getPrivateSettingEntity)){
            P.showSettingInfo(getPrivateSettingEntity.getObj());
        }else {
            ToastUtil.showCentertoast(getPrivateSettingEntity.getMsg());
        }
    }

    @Override
    public void savePrivacySetting(Map<String, Object> requestArgsMap,final SwitchButton view) {
        model.savePrivacySetting(requestArgsMap, new BaseIRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                String messae = result.getMessage();
                BaseEntity baseEntity = FastJsonUtil.fromBean(messae,BaseEntity.class);
                if (!OtherUtil.checkRequestStatus(baseEntity)) {
                    view.setChecked(false);
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }
        });
    }
}
